Karura Income

Row

7D Fee Income

7D Sum FeeIncome_KAR 7D Sum Stability_Fee_USD 7D Sum CDP_Penalty_Fee_USD
70.0778 131.7587 0

Row

30D Fee Income

30D Sum FeeIncome_KAR 30D Sum Stability_Fee_USD 30D Sum CDP_Penalty_Fee_USD
446.3217 524.3505 4,033.05

Row

Daily Fee Data

date Block Time Balance FeeIncome Stability_Fee_USD CDP_Penalty_Fee_USD
2022-06-18 2,116,723 2022-06-18 19:55:00 5,884.218 NA 28.483390 0.00
2022-06-19 2,123,103 2022-06-19 19:53:18 5,898.752 14.534391 29.382536 0.00
2022-06-20 2,129,496 2022-06-20 19:52:00 5,908.977 10.224336 41.454262 0.00
2022-06-21 2,135,921 2022-06-21 19:58:48 5,925.738 16.761595 29.570867 0.00
2022-06-22 2,142,277 2022-06-22 19:57:36 5,933.906 8.167393 29.759649 0.00
2022-06-23 2,148,574 2022-06-23 19:36:06 5,944.374 10.468289 21.291394 0.00
2022-06-24 2,155,138 2022-06-24 19:58:18 5,956.988 12.613710 6.003383 0.00
2022-06-25 2,161,561 2022-06-25 19:59:06 5,971.290 14.301985 29.130712 0.00
2022-06-26 2,168,012 2022-06-26 19:59:24 6,007.855 36.565365 28.190067 0.00
2022-06-27 2,173,903 2022-06-27 19:57:24 6,024.226 16.370824 30.174667 0.00
2022-06-28 2,179,758 2022-06-28 19:59:18 6,049.224 24.997791 19.024682 0.00
2022-06-29 2,185,604 2022-06-29 19:59:42 6,077.593 28.369380 9.933304 0.00
2022-06-30 2,191,478 2022-06-30 19:55:30 6,115.562 37.968674 10.044173 4,033.05
2022-07-01 2,197,146 2022-07-01 19:58:42 6,128.033 12.470743 14.340214 0.00
2022-07-02 2,202,850 2022-07-02 19:58:48 6,174.563 46.530405 7.606983 0.00
2022-07-03 2,208,543 2022-07-03 19:24:24 6,193.132 18.569154 7.018818 0.00
2022-07-04 2,214,452 2022-07-04 19:57:24 6,207.424 14.292222 16.586245 0.00
2022-07-05 2,220,334 2022-07-05 19:55:12 6,227.613 20.188744 3.446158 0.00
2022-07-06 2,226,214 2022-07-06 19:52:18 6,236.555 8.941460 5.875040 0.00
2022-07-07 2,232,058 2022-07-07 19:52:06 6,241.291 4.736231 5.046345 0.00
2022-07-08 2,237,966 2022-07-08 19:58:12 6,247.928 6.637194 12.837616 0.00
2022-07-09 2,243,856 2022-07-09 19:52:36 6,255.338 7.410115 1.754520 0.00
2022-07-10 2,249,768 2022-07-10 19:56:18 6,260.462 5.123861 5.636786 0.00
2022-07-11 2,255,712 2022-07-11 19:59:12 6,267.181 6.719433 33.924488 0.00
2022-07-12 2,261,598 2022-07-12 19:57:54 6,277.334 10.152450 36.765002 0.00
2022-07-13 2,267,293 2022-07-13 19:59:42 6,289.319 11.985092 27.967843 0.00
2022-07-14 2,273,076 2022-07-14 19:46:48 6,296.251 6.931851 9.763748 0.00
2022-07-15 2,278,952 2022-07-15 19:59:24 6,304.134 7.882745 4.609661 0.00
2022-07-16 2,284,855 2022-07-16 19:55:42 6,313.013 8.879331 8.471537 0.00
2022-07-17 2,290,691 2022-07-17 19:57:54 6,330.540 17.526900 10.256404 0.00

Karura Balances

Row

Balance for account qmmNufxeWaAUy17XBWGc1q4n2dkdxNS2dPkhtzCZRoExdAs

tokenId timestamp free price balanceUSD
LKSM 2022-07-18T00:00:00 821.4708 7.186054 5,903.134

Row

Balance for account qmmNufxeWaAUp4SVa1Vi1owrzP1xhR6XKdorEcck17RF498

tokenId timestamp free price balanceUSD
LKSM 2022-07-18T00:00:00 821.4708 7.186054 5,903.134
---
title: "Acala / Karura Income Statement"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    social: menu
    source_code: embed
params:
  network: Karura
---

```{css custom1, echo=FALSE}
.dataTables_scrollBody {
    max-height: 100% !important;
}
```

```{r global, include=FALSE}
library(knitr)
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

library(dygraphs)
library(kableExtra)
library(formattable)
library(lubridate)
library(flexdashboard)
library(DT)
library(subscanr)
library(formattable)
library(ghql)
x <- GraphqlClient$new()

# Helper function to concat
`%+%` <- function(a, b) paste0(a, b)

library(reticulate)
# use_python("/opt/homebrew/bin/python3.9")

network <- params$network

# define constants
if (tolower(network) == 'acala') {
  dex_endpoint <- "https://api.subquery.network/sq/AcalaNetwork/acala-dex-subql"
  nativeToken <- "ACA"
  api_url = 'wss://acala-rpc-0.aca-api.network'
  addr1 <- "23M5ttkmR6Kco5p3LFGKMpMv4zvLkKdUQWW1wGGoV8zDX3am"
  addr2 <- "23M5ttkmR6KcnvsNJdmYTpLo9xfc54g8uCk55buDfiJPon69"

} else if (tolower(network) == 'karura') {
  dex_endpoint <- "https://api.subquery.network/sq/AcalaNetwork/karura-dex-subql"
  nativeToken <- "KAR"
  api_url = 'wss://karura.polkawallet.io'
  addr1 <- "qmmNufxeWaAUy17XBWGc1q4n2dkdxNS2dPkhtzCZRoExdAs"
  addr2 <- "qmmNufxeWaAUp4SVa1Vi1owrzP1xhR6XKdorEcck17RF498"
  
}

# Get prices & blocks
method <- "tokenDailyData"
edges <- "tokenId price timestamp updateAtBlockId"
prices <- get_graph(dex_endpoint, method, edges, window = 31, filter = '')
prices[, block := as.numeric(updateAtBlockId) - 1]
prices[, date := as.Date(timestamp) - 1]
# prices <- merge(prices, tokens, by.x = "tokenId", by.y = "Token")
# prices[, adj := 10**as.numeric(decimals)]
prices[, price := as.numeric(price) / 10**18]
prices[, max_block := max(as.numeric(updateAtBlockId)), by = tokenId]
prices[, tokenId := fixToken(tokenId)]
# prices[tokenId=="KSM"]

# Get the max block for each date and feed that to the python code
minDate <- today() - 32
history <- prices[, max(block), by = date] %>%
  setorder(date)
history[, blockDelta := V1 - shift(V1)]
block_history <- history[blockDelta > 5000 & date > minDate, V1]
write.csv(block_history, "history.csv", row.names = FALSE)
  
```

```{python, include=FALSE}
from substrateinterface import SubstrateInterface

import pandas as pd
import numpy as np
from datetime import date
history = pd.read_csv('history.csv', index_col = None)
block_history = history['x']
# block_id = block_history[30]


# pull in the balance for `acct` based on each block in `history`
def get_fees(url, acct, block_history):
    data = []
    try:
        substrate = SubstrateInterface(url)
        for j in block_history:
            hash = substrate.get_block_hash(block_id = j)        
            timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
            block = substrate.get_block_number(hash)
            # result = substrate.query('System', 'Account', params = [acct], block_hash = hash)
            # free = result.value['data']['free']
            balance_info = substrate.get_runtime_state(
                module='System',
                storage_function='Account',
                params=[acct],
                block_hash=hash
            ).get('result')
            balance = balance_info.get('data').get('free', 0) # / 10**12
            outi = {"Block": block, "Time": timestamp, 'Balance': balance}
            data.append(outi)
    except Exception as e:
        balance = None
    return data
            
# url = 'wss://acala-rpc-0.aca-api.network'
url = r.api_url
acct = '23M5ttkmR6KcoTAAE6gcmibnKFtVaTP5yxnY8HF1BmrJ2A1i'
fees_api = get_fees(url, acct, block_history)
fees_api = pd.DataFrame(fees_api)
fees_api['Time'] = pd.to_datetime(fees_api['Time'],unit='ms')

```

```{r treasury, cache = TRUE, include=FALSE}

# Treasury account from Python API fees
fees <- py$fees_api %>%
  as.data.table
fees[, Balance := as.numeric(Balance) / 10**12]

fees[, FeeIncome := Balance - shift(Balance, 1)]
fees[, date := as.Date(Time)]

# Stability fee
collaterParams <- getLoansCollateralParams_acala_loan(network)
collaterParams <- collaterParams[!duplicated(collateral.id), .(collateral.id, APR)]
if (network == 'Karura') {
  pos <- getLoansDailyPositions_acala_loan(network, window = 31, staging = FALSE) 
} else {
  pos <- getLoansDailyPositions_acala_loan(network, window = 31, staging = TRUE) 
}
pos <- pos[, .(timestamp, collateral.id, debitVolumeUSD, Date)]
pos <- merge(pos, collaterParams, by = "collateral.id", all.x = TRUE)
pos[, dailyAPR := APR / 365]
pos[, dailyFee := as.numeric(debitVolumeUSD) * dailyAPR]
fwrite(pos, file = network %+% "_stability_rawdata.csv")

dailyStability <- pos[, sum(dailyFee), by = Date] %>%
  setnames("V1", "Stability_Fee_USD") %>%
  setorder(Date)
fees <- merge(fees, dailyStability, by.x = 'date', by.y = 'Date', all.x = TRUE)

if (network == 'Karura') {
  # get bad debt penalty from subquery
  cdp2 <- getLiquidateUnsafeCDP_acala_loan(network, window = 31, staging = FALSE)
} else {
  cdp2 <- getLiquidateUnsafeCDP_acala_loan(network, window = 31, staging = TRUE)
}
cdp2[, block.id := as.numeric(block.id)]
cdp3 <- cdp2[, .(Date, badDebitVolumeUSD)]
fwrite(cdp3, file = network %+% "_cdp_rawdata.csv")

cdp4 <- cdp3[, sum(as.numeric(badDebitVolumeUSD)) * .15, by = Date] %>%
  setnames("V1", "CDP_Penalty_Fee_USD")
fees <- merge(fees, cdp4, by.x = 'date', by.y = 'Date', all.x = TRUE)
fees[is.na(CDP_Penalty_Fee_USD), CDP_Penalty_Fee_USD := 0]
liquidations <- cdp2[, .(block.id, timestamp, collateral.id, collateralVolumeUSD, badDebitVolumeUSD)]

# } else {
# 
#   # get bad debt penalty from subquery
#   cdp2 <- getLiquidateUnsafeCDP_acala_loan(network, window = 31, staging = TRUE)
#   cdp2[, block.id := as.numeric(block.id)]
#   cdp3 <- cdp2[, .(Date, badDebitVolumeUSD)]
#   fwrite(cdp3, file = network %+% "_cdp_rawdata.csv")
# 
#   cdp4 <- cdp3[, sum(as.numeric(badDebitVolumeUSD)) * .15, by = Date] %>%
#     setnames("V1", "CDP_Penalty_Fee_USD")
#   both <- merge(cdp_daily, cdp4, by.x = "date", by.y = "Date", all = TRUE)
#   
#   d <- list()
#   for (i in 1:20) {
#     cdp_rawdata <- get_subscan_events(nobs = 100, network = network, module = 'cdpengine', call = 'LiquidateUnsafeCDP', start_page = i, extract = TRUE)
#     cdp <- cdp_rawdata$cdpengine_LiquidateUnsafeCDP
#     cdp[, date := as.Date(time)]
#     d[[i]] <- cdp[time >= minDate]
#     if (min(cdp$date) < minDate) break
#   }
#   cdp <- rbindlist(d)
#   cdp[, CurrencyId := fixToken(CurrencyId)]
#   cdp[, block_num := as.numeric(block_num)]
#   cdp[, BadDebtValue := as.numeric(BadDebtValue) / 10**12]
#   cdp[, CollateralAmount := as.numeric(CollateralAmount) / 10**12]
#   fwrite(cdp, file = network %+% "_cdp_rawdata.csv")
# 
#   cdp_daily <- cdp[, sum(as.numeric(BadDebtValue)) * .15, by = date] %>%
#     setnames("V1", "CDP_Penalty_Fee_USD")
#   fees <- merge(fees, cdp_daily, by.x = 'date', by.y = 'date', all.x = TRUE)
#   liquidations <- cdp[, .(block_num, time, CurrencyId, CollateralAmount, BadDebtValue)]
# 
# }
fees[is.na(CDP_Penalty_Fee_USD), CDP_Penalty_Fee_USD := 0]
fees <- tail(fees, 30)
fees7 <- tail(fees, 7)

sum30 <- fees[, .(sum(FeeIncome, na.rm = TRUE), 
                  sum(Stability_Fee_USD, na.rm = TRUE),
                  sum(CDP_Penalty_Fee_USD, na.rm = TRUE))] %>%
  setnames(c("V1", "V2", "V3"), 
           c("30D Sum FeeIncome_" %+% nativeToken, "30D Sum Stability_Fee_USD", "30D Sum CDP_Penalty_Fee_USD"))

sum7 <- fees7[, .(sum(FeeIncome, na.rm = TRUE), 
                  sum(Stability_Fee_USD, na.rm = TRUE),
                  sum(CDP_Penalty_Fee_USD, na.rm = TRUE))] %>%
  setnames(c("V1", "V2", "V3"), 
           c("7D Sum FeeIncome_" %+% nativeToken, "7D Sum Stability_Fee_USD", "7D Sum CDP_Penalty_Fee_USD"))

# test <- merge(cdp[, .(date, CurrencyId, block_num, BadDebtValue)],
#               cdp2[, .(Date, collateral.id, block.id, badDebitVolumeUSD)],
#               by.x = c('CurrencyId', 'block_num'),
#               by.y = c('collateral.id', 'block.id'),
#               all = TRUE)
# View(test)


# liquid staking fee
filter <- ' filter: {accountId: {equalTo: "' %+% addr1 %+% '"}}'
dat1 <- getDailyAccountBalance_acala_token(network, window = 31, filter = filter)
price <- prices[updateAtBlockId == max_block, .(tokenId, price)]
dat1 <- merge(dat1, 
              prices[, .(tokenId, timestamp, price)], 
              by = c("timestamp","tokenId"), 
              all.x = TRUE)
dat1[, balanceUSD := free * price]
data1 <- dat1[free > 1, .(tokenId, timestamp, free, price, balanceUSD)]


# builtin1 <- try(subscanr::get_subscan_account_tokens(network = network, addr))
# while (inherits(builtin1, "try-error")) {
#   Sys.sleep(3)
#   builtin1 <- try(subscanr::get_subscan_account_tokens(network, addr))
# }
# time <- as.POSIXct(builtin1$generated_at, origin = "1970-01-01", tz = 'UTC')
# data1 <- builtin1$data$builtin %>%
#   as.data.table
# data1[, balance := as.numeric(balance) / 10**as.numeric(decimals)]
# price <- prices[updateAtBlockId == max_block, .(tokenId, price)]
# data1 <- merge(data1, price, by.x = "symbol", by.y = "tokenId", all.x = TRUE)
# data1[, balanceUSD := balance * price]
# data1[, .(symbol, balance, price, balanceUSD)]

# Stablecoin stability fee + liquidation fee, aUSD balance
filter <- ' filter: {accountId: {equalTo: "' %+% addr1 %+% '"}}'
dat2 <- getDailyAccountBalance_acala_token(network, window = 31, filter = filter)
price <- prices[updateAtBlockId == max_block, .(tokenId, price)]
dat2 <- merge(dat2, 
              prices[, .(tokenId, timestamp, price)], 
              by = c("timestamp","tokenId"), 
              all.x = TRUE)
dat2[, balanceUSD := free * price] 
setorder(dat2, tokenId, timestamp)
data2 <- dat2[free > 1, .(tokenId, timestamp, free, price, balanceUSD)]

# addr <- "qmmNufxeWaAUp4SVa1Vi1owrzP1xhR6XKdorEcck17RF498"; network = "Karura"
# builtin2 <- try(subscanr::get_subscan_account_tokens(network, addr))
# while (inherits(builtin2, "try-error")) {
#   Sys.sleep(3)
#   builtin2 <- try(subscanr::get_subscan_account_tokens(network, addr))
# }
# time <- as.POSIXct(builtin2$generated_at, origin = "1970-01-01", tz = 'UTC')
# data2 <- builtin2$data$builtin %>% 
#   as.data.table
# data2[, balance := as.numeric(balance) / 10**as.numeric(decimals)]
# price <- prices[updateAtBlockId == max_block, .(tokenId, price)]
# data2 <- merge(data2, price, by.x = "symbol", by.y = "tokenId", all.x = TRUE)
# data2[, balanceUSD := balance * price]
# data2[, .(symbol, balance, price, balanceUSD)]

# It should have a section display the current holding and dollar value,
# last 7 day income, last 30 day income, some charts if you have time
```

# `r network` Income {.tabset}

Row
----

### 7D Fee Income

```{r sum7}

knitr::kable(sum7, escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

```

Row
----

### 30D Fee Income

```{r sum30}

knitr::kable(sum30, escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

dat <- fees[, .(date, FeeIncome, Stability_Fee_USD, CDP_Penalty_Fee_USD)]
main <- network %+% " Daily Fees"
dygraph(dat, main = main)  %>% 
    dySeries("FeeIncome", stepPlot = TRUE, fill = TRUE)

rm(dat)

```

Row
----

### Daily Fee Data

```{r daily}

knitr::kable(fees, escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

```

# `r network` Balances {.tabset}

Row
----

### Balance for account `r addr1`

```{r data1}

knitr::kable(data1[timestamp == max(timestamp)], escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

dat <- data1[, .(timestamp, tokenId, balanceUSD)]
dat[, timestamp := as.Date(timestamp)]
us <- unique(dat$tokenId)

main <- network %+% " " %+% us[1] %+% " Treasury in " %+% addr1
dygraph(dat[tokenId == us[1], .(timestamp, balanceUSD)], main = main)  %>% 
    dySeries("balanceUSD", stepPlot = TRUE, fill = TRUE)

if (length(us) > 1) {
  main <- network %+% " " %+% us[2] %+% " Treasury in " %+% addr1
  dygraph(dat[tokenId == us[2], .(timestamp, balanceUSD)], main = main)  %>% 
      dySeries("balanceUSD", stepPlot = TRUE, fill = TRUE)
}

rm(dat)

```

Row
----

### Balance for account `r addr2`

```{r data2}

knitr::kable(data2[timestamp == max(timestamp)], escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

dat <- data2[, .(timestamp, tokenId, balanceUSD)]
dat[, timestamp := as.Date(timestamp)]
us <- unique(dat$tokenId)

main <- network %+% " " %+% us[1] %+% " Treasury in " %+% addr2
dygraph(dat[tokenId == us[1], .(timestamp, balanceUSD)], main = main)  %>% 
    dySeries("balanceUSD", stepPlot = TRUE, fill = TRUE)

if (length(us) > 1) {
  main <- network %+% " " %+% us[2] %+% " Treasury in " %+% addr2
  dygraph(dat[tokenId == us[2], .(timestamp, balanceUSD)], main = main)  %>% 
      dySeries("balanceUSD", stepPlot = TRUE, fill = TRUE)
}

rm(dat)

```